using System;
using System.Collections.Generic;
using System.Text;

namespace GriffinNav.GPS.NMEA
{
    // Recommended Minimum sentence C
    internal struct GPRMC
    {
        //$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A 
        //     RMC          Recommended Minimum sentence C 
        //     123519       Fix taken at 12:35:19 UTC 
        //     A            Status A=active or V=Void. 
        //     4807.038,N   Latitude 48 deg 07.038' N 
        //     01131.000,E  Longitude 11 deg 31.000' E 
        //     022.4        Speed over the ground in knots 
        //     084.4        Track angle in degrees True 
        //     230394       Date - 23rd of March 1994 
        //     003.1,W      Magnetic Variation 
        //     *6A          The checksum data, always begins with *

        internal static GPRMC Empty = new GPRMC(string.Empty);
        internal const string PREFIX = "$GPRMC,";
        
        private string[] mRmcParams;

        private Velocity mVelocity;

        internal GPRMC(string rmcString)
        {
            mVelocity = new Velocity();
            mRmcParams = null;

            Update(rmcString);
        }

        internal Velocity Velocity
        {
            get
            {
                return mVelocity;
            }
        }

        internal void Update(string rmcString)
        {
            // check for a value
            if (!string.IsNullOrEmpty(rmcString))
            {
                // remove sentence prefix and leading comma
                mRmcParams = rmcString.Remove(0, PREFIX.Length).Split(new char[] { ',' });

                // check for speed and direction values
                if (!string.IsNullOrEmpty(mRmcParams[6]) && !string.IsNullOrEmpty(mRmcParams[7]))
                {
                    // update speed (knots) and direction
                    mVelocity.Update(decimal.Parse(mRmcParams[6]), decimal.Parse(mRmcParams[7]));
                }
            }
        }
    }
}
